package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallOrderB2cEntity;
import net.lahuobao.mall.entity.admin.MallOrderO2oEntity;
import net.lahuobao.mall.entity.admin.MallOrderVirtualEntity;
import net.lahuobao.mall.entity.admin.MallOrderdetailEntity;
import net.lahuobao.mall.service.admin.MallOrderdetailServiceI;
import net.lahuobao.mall.service.admin.MallRegionServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("mallOrderdetailService")
@Transactional
public class MallOrderdetailServiceImpl extends CommonServiceImpl implements MallOrderdetailServiceI {

    @Autowired
    private MallRegionServiceI regionService;
    /**
     * 查询订单详情
     * @param orderId
     * @return
     */
    @Override
    public List<MallOrderdetailEntity> selectByOrderIdAndType(Integer orderId, Integer orderType) {

        List<MallOrderdetailEntity>  results = new ArrayList<MallOrderdetailEntity>();

        results = super.findByProperty(MallOrderdetailEntity.class, "orderid", orderId);

        for(MallOrderdetailEntity orderdetailEntity : results){
            //线上商城订单
            if(orderType == 0){
                MallOrderB2cEntity b2c = super.findUniqueByProperty(MallOrderB2cEntity.class, "orderid", orderId);
                String[] str = regionService.getAddressByCode(Integer.parseInt(b2c.getAreacode())).split(";");
                String reStr = "";
                for (int i = 0; i < str.length; i++) {
                    reStr += str[i].split("_")[0];
                }
                b2c.setAreacodeStr(reStr);
                orderdetailEntity.setMallOrderB2cEntity(b2c);
            }
            //本地服务订单
            if(orderType == 1){
                MallOrderO2oEntity o2o = super.findUniqueByProperty(MallOrderO2oEntity.class, "orderid", orderId);
                orderdetailEntity.setMallOrderO2oEntity(o2o);
            }

            //在线充值服务订单
            if(orderType == 2){
                MallOrderVirtualEntity virtual = super.findUniqueByProperty(MallOrderVirtualEntity.class, "orderid", orderId);
                orderdetailEntity.setMallOrderVirtualEntity(virtual);
            }
        }

        return results;
    }
}